package cn.cytong.util.ext

private val HEX_CHARS = "0123456789ABCDEF"

fun String.toHexBytes(): ByteArray {
    val str = this.replace(" ", "")
    val len = str.length
    val data = ByteArray(len / 2)
    var i = 0
    while (i < len) {
        data[i / 2] = ((Character.digit(str[i], 16) shl 4)
                + Character.digit(str[i + 1], 16)).toByte()
        i += 2
    }
    return data
}

fun String.grouped(len: Int = 2): List<String> {
    val list = mutableListOf<String>()
    repeat(this.length / len, { times ->
        list.add(this.substring(len * times, Math.min(this.length, len * (times + 1))))
    })

    return list
}